import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Put, UseInterceptors, UploadedFile } from '@nestjs/common';
import { ProjectService } from './project.service';
import { CreateProjectDto } from './dto/create-project.dto';
import { UpdateProjectDto } from './dto/update-project.dto';
import { ListProjectDto } from './dto/list-project.dto';
import { User, UserDto } from '@/module/system/user/user.decorator';
import { FileInterceptor } from '@nestjs/platform-express';
import { UploadService } from '@/module/upload/upload.service';
import { ResultData } from '@/common/utils/result';
import { ListAnnexDto } from './dto/list-annex.dto';
import {Express} from 'express'

@Controller('business/project')
export class ProjectController {
    constructor(
        private readonly projectService: ProjectService,
        private readonly uploadService: UploadService
    ) { }

    @Post()
    create(@User() user: UserDto, @Body() createProjectDto: CreateProjectDto) {
        return this.projectService.create(user.user, createProjectDto);
    }

    @Get("/list")
    findAll(@Query() query: ListProjectDto) {
        return this.projectService.findAll(query);
    }

    @Get('/findRepeat')
    findRepeat(@Query('projectName') projectName: string) {
        return this.projectService.findFirst(projectName);
    }

    @Put()
    update(@User() user: UserDto, @Body() updateProjectDto: UpdateProjectDto) {
        return this.projectService.update(user.user, updateProjectDto);
    }
    @Put('/status')
    updateStatus(@User() user: UserDto, @Body() updateStatusDto: { projectId: number, status: string }) {
        return this.projectService.updateStatus(user.user, updateStatusDto);
    }

    @Delete(':id')
    remove(@Param('id') ids: string) {
        console.log(ids, 'ids');
        const projectIds = ids.split(',').map((id) => +id);
        return this.projectService.remove(projectIds);
    }

    @Post('/importAnnex/:id')
    @UseInterceptors(FileInterceptor('file'))
    async createAnnex(@Param('id') id: string, @UploadedFile() file: Express.Multer.File, @Body('type') type: string, @User() user: UserDto) {
        console.log(id, file, type);
        const res = await this.uploadService.singleFileUpload(user.user.userId, file);
        if (res instanceof ResultData) {
            return res
        }
        console.log(res, '上传成功');
        return await this.projectService.createAnnex(+id, res.uploadId, type);
    }

    @Get('/annex/:id')
    async findAllAnnex(@Query() query: ListAnnexDto, @Param("id") id: number) {
        return await this.projectService.findAllAnnex(+id, query);
    }
    @Delete('/annex/:ids')
    async removeAnnex(@Param("ids") ids: string) {
        const uploadIds = ids.split(',').map((id) => id);
        return await this.projectService.removeAnnex(uploadIds);
    }

    @Get('/score')
    async findMemberAndScore() {
        return await this.projectService.findMemberAndScore();
    }
}
